package startup

import (
	"fmt"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/http_server/middleware"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/http_server/router"
	"git.mycaigou.com/ycg/go2sky-plugins/gin/v3"
	"git.mycaigou.com/ycg/stark-extend/go2skyplugins"

	"git.myscrm.cn/golang/common/ykenv"
	"github.com/gin-gonic/gin"
)

const WELCOME = "micro-gfyx-mp-api"

func init() {
	gin.SetMode(gin.DebugMode)
	if ykenv.IsTestMode() {
		gin.SetMode(gin.TestMode)
	}
	if ykenv.IsProdMode() || ykenv.IsReleaseMode() {
		gin.SetMode(gin.ReleaseMode)
	}
}

// RegisterHttpRoute 此处注册 gin 接口
// 路由注册需按照规范创建，规则如下: 版本号/上下文/聚合，动作的路径放在 router 中实现，与 pc bbf 保持一致，有利于代码的复用
// 领域划分的地址，参考链接：https://confluence.mysre.cn/pages/viewpage.action?pageId=90257722
func RegisterHttpRoute(r *gin.Engine) error {
	r.Use(gin.Recovery())
	r.Use(middleware.Cors())
	r.Use(middleware.AccessLogs())
	r.Use(middleware.LimitIP())

	// 链路接入
	tracer, err := go2skyplugins.GetTracer()
	if err != nil {
		fmt.Println("go2skyplugins.GetTracer err:", err.Error())
	} else {
		r.Use(v3.Middleware(r, tracer)) // r是*gin.Engine
	}
	healthGroup := r.Group("/")
	router.RegisterHealthRouters(healthGroup)
	r.Any("/", func(c *gin.Context) {
		c.JSON(0, map[string]interface{}{
			"message": WELCOME,
		})
	})

	// 开放api
	open := r.Group("/api/v1/open")
	open.Use(middleware.OpenAuthorize())
	router.OpenRegisterRouters(open)

	// 小程序登录
	mpUser := r.Group("/api/v1/mp-user")
	router.MpUserRegisterRouters(mpUser)
	//小程序获取qrcode二维码信息

	miniProgram := r.Group("/v1/mini-program")
	router.MiniProgramRegisterRouters(miniProgram)

	//用户同步
	syncUser := r.Group("/api/v1/sync")
	syncUser.Use(middleware.SyncUserAuthorize())
	router.SyncUserRegisterRouters(syncUser)

	// 公用模块
	common := r.Group("/v1/common")
	common.Use(middleware.Authorize())
	router.CommonRegisterRouters(common)

	// 公用模块alias
	commonAlias := r.Group("/api/v1/common")
	common.Use(middleware.Authorize())
	router.CommonRegisterRouters(commonAlias)

	//找客户（待废弃）
	customer := r.Group("/api/v1/customer")
	customer.Use(middleware.Authorize())
	router.CustomerRegisterRouters(customer)

	//报备
	report := r.Group("/api/v1/report")
	report.Use(middleware.Authorize())
	router.ReportRegisterRouters(report)

	//订阅（线索雷达订阅）
	subscription := r.Group("/api/v1/subscription")
	subscription.Use(middleware.Authorize())
	router.SubscriptionRegisterRouters(subscription)

	//不需要登陆
	anonymity := r.Group("/api/v1/anonymity")
	router.AnonymityRegisterRouters(anonymity)

	//找工程单位
	supplier := r.Group("/api/v1/supplier")
	supplier.Use(middleware.Authorize())
	router.SupplierRegisterRouters(supplier)

	// 联系人
	contactRoute := r.Group("/api/v1/contact")
	contactRoute.Use(middleware.Authorize())
	router.ContactRegisterRouters(contactRoute)

	// 备选项
	optionGroup := r.Group("/v1/option")
	optionGroup.Use(middleware.Authorize())
	router.OptionRegisterRouters(optionGroup)

	// 验证码
	verifyGroup := r.Group("/v1/verify")
	//verifyGroup.Use(middleware.Authorize())
	router.VerifyRegisterRouters(verifyGroup)

	// 商机上下文，包含 招中标 项目 客户 联系人
	// 按新的领域划分路径：/版本号/领域/聚合/动作
	businessOpportunityGroup := r.Group("/v1/business_opportunity")
	businessOpportunityGroup.Use(middleware.Authorize())
	router.BusinessOpportunityRegisterRouters(businessOpportunityGroup)

	clueManageGroup := r.Group("/v1/clue/clue-manage")
	clueManageGroup.Use(middleware.Authorize())
	router.ClueManageRegisterRouters(clueManageGroup)

	// 用户信息
	baseGroup := r.Group("/v1/base")
	baseGroup.Use(middleware.Authorize())
	router.UserRegisterRouters(baseGroup)
	return nil
}
